const fs = require('fs')
const path = require('path')
const net = require('net')

var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var jsonParser = bodyParser.json()
app.use(express.static('./'))


let STATUS = 0
let TEMP = 0


app.get('/', function(req,rep){
	var data = fs.readFileSync('./index.html')
	rep.end(data.toString())
})

app.get('/open', function(req,rep){
	const socket = net.createConnection({ port: 7777 }, () => {
	  socket.write(JSON.stringify({cmd : 'open', data : 'CTRL:11\r\n'}));
	  socket.end()
	})
	
	rep.end('ok')
	
})

app.get('/close', function(req,rep){
	const socket = net.createConnection({ port: 7777 }, () => {
	  socket.write(JSON.stringify({cmd : 'close', data : 'CTRL:10\r\n'}));
	  socket.end()
	})
	
	rep.end('ok')
	
})

app.get('/getdata', function(req,rep){
	
	rep.end(JSON.stringify({status : STATUS, temp : TEMP}))
	
})

app.post('/receive', jsonParser, function(req, rep){
	let params = req.body
	
	console.log(params)
	
	STATUS = params.status
	TEMP = params.temp
	
	rep.end('ok')
})


app.listen('8080')